<?php
    include 'header.php';?>  
    <div id="registerform"><?php 
    if ( isset( $_POST['username'] ) && isset( $_POST['password'] ) && isset( $_POST['passwordrep'] )&& isset( $_POST['name'] ) && isset( $_POST['surname'] ) && !empty( $_POST['username'] ) && !empty( $_POST['password'] ) && !empty( $_POST['passwordrep'] )  && !empty( $_POST['name'] )  && !empty( $_POST['surname'] ) ) {
    	if ( !is_numeric($_POST['username']) ) {
			if ( $_POST['password'] == $_POST['passwordrep']) {
				$username = $_POST['username'];
				$username = htmlspecialchars(addslashes($username));
				$password = md5(sha1($_POST['password']));
				$name = $_POST['name'];
				$name = htmlspecialchars(addslashes($name));
				$surname = $_POST['surname'];
				$surname = htmlspecialchars(addslashes($surname));
				$email = $_POST['email'];
				$email = htmlspecialchars(addslashes($email));
				$genre = $_POST['genre'];
				$datetime = date("Y/m/d-H/i/s");
			
				// check if username exists
				$res = mysql_query(
					'SELECT
						username
					FROM
						users
					WHERE
						username = "' . $username . '";'
				);
				if ( mysql_num_rows($res) == 0 ) {
					mysql_query(
						'INSERT INTO
							users
						SET
							name = "' . $name . '",
							surname = "' . $surname . '",
							username = "' . $username . '",
							password = "' . $password . '",
							email = "' . $email . '",
							dob = "'. $datetime .'",
							genre = "' . $genre . '";'
					);
					$_SESSION['username'] = $username;
					$_SESSION['userid'] = mysql_insert_id();
					echo "<script> window.location = \"./reg_success.php\"</script>";
				}
				else {
					echo "<script> window.location = \"./register.php?exists=yes\"</script>";
				}
			}
			else {
				echo "<script> window.location = \"./register.php?wrongrepeat=yes\"</script>";
			}
		}
		else {
			echo "<script> window.location = \"./register.php?numeric=yes\"</script>";
		}
	}
    else {
        echo "<script> window.location = \"./register.php?missing=yes\"</script>";
    }?>
    </div>
    <?php
    include 'footer.php'
?>
